package ${basePackage}.api.system;

import ${basePackage}.api.BaseController;
import ${basePackage}.biz.system.SystemJobSnippetTriggerBiz;
import ${basePackage}.biz.system.dto.TriggerJobDTO;
import ${basePackage}.biz.system.dto.TriggerJobSnippetDTO;
import ${basePackage}.core.annotation.pr.PreventRepeat;
import ${basePackage}.core.annotation.trace.Trace;
import ${basePackage}.core.constants.Constants;
import ${basePackage}.core.model.ApiResponse;
import ${basePackage}.core.model.PageWrap;
import ${basePackage}.core.model.PageData;
import ${basePackage}.core.utils.Utils;
import ${basePackage}.dao.system.model.SystemJobSnippet;
import ${basePackage}.service.system.SystemJobSnippetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;    
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "定时任务片段")
@RestController
@RequestMapping("/system/job/snippet")
public class SystemJobSnippetController extends BaseController {

    @Autowired
    private SystemJobSnippetService systemJobSnippetService;

    @Autowired
    private SystemJobSnippetTriggerBiz systemJobSnippetTriggerBiz;

    @Trace
    @ApiOperation("手动触发")
    @PostMapping("/trigger")
    @RequiresPermissions("system:job:trigger")
    public ApiResponse triggerJob (@RequestBody TriggerJobSnippetDTO dto) {
        Utils.ThreadPool.start(() -> {
            dto.setTriggerType(Constants.Job.TriggerType.MANUAL.getCode());
            systemJobSnippetTriggerBiz.trigger(dto);
        });
        return ApiResponse.success(null);
    }

    @ApiOperation("根据ID删除")
    @GetMapping("/delete/{id}")
    @RequiresPermissions("system:job:snippet:delete")
    public ApiResponse deleteById(@PathVariable Integer id) {
        systemJobSnippetService.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    @RequiresPermissions("system:job:snippet:delete")
    public ApiResponse deleteByIdInBatch(@RequestParam String ids) {
        systemJobSnippetService.deleteByIdInBatch(this.getIdList(ids));
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @RequiresPermissions("system:job:snippet:query")
    public ApiResponse<PageData<SystemJobSnippet>> findPage (@RequestBody PageWrap<SystemJobSnippet> pageWrap) {
        return ApiResponse.success(systemJobSnippetService.findPage(pageWrap));
    }
}
